package zsc.ruanc.practicaltraining.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import zsc.ruanc.practicaltraining.model.result.JsonResult;
import zsc.ruanc.practicaltraining.model.vo.MailboxVo;
import zsc.ruanc.practicaltraining.service.RabbitMQService;
import zsc.ruanc.practicaltraining.utils.ResultUtils;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

/**
 * 消息控制器
 */
@RestController
@RequestMapping("/rabbitmq")
@Validated
@Api(tags = "信箱模块")
public class RabbitMQController {

    @Autowired
    private RabbitMQService rabbitMQService;


    @GetMapping("/select")
    @ApiOperation(value = "获得信箱信息（分页）", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cur", value = "当前页", dataType = "Integer", paramType = "query", defaultValue = "1"),
            @ApiImplicitParam(name = "size", value = "页大小", dataType = "Integer", paramType = "query", defaultValue = "10"),
    })
    public JsonResult select(@Min(message = "参数cur不能小于0", value = 0) @RequestParam(value = "cur", defaultValue = "1") Integer cur,
                             @Min(message = "参数size不能小于1", value = 1) @RequestParam(value = "size", defaultValue = "10") Integer size,
                             HttpServletRequest httpServletRequest) {
        int id = (int)httpServletRequest.getAttribute("id");

        return ResultUtils.success(rabbitMQService.select(cur, size, id));

    }

    @GetMapping("/delete/{id}")
    @ApiOperation(value = "删除一条记录" , httpMethod = "GET")
    public JsonResult delete(@NotNull(message = "不能为空") @Min(message = "参数不能小于1", value = 1) @PathVariable(value = "id") Integer id,
                             HttpServletRequest httpServletRequest) {

        int userId = (int) httpServletRequest.getAttribute("id");

        return ResultUtils.success(rabbitMQService.delete(id, userId), "删除成功");
    }

    @GetMapping("/unreadCount")
    @ApiOperation(value = "查看未读记录" , httpMethod = "GET")
    public JsonResult unread(HttpServletRequest httpServletRequest) {

        int userId = (int) httpServletRequest.getAttribute("id");

        return ResultUtils.success(rabbitMQService.selectUnread(userId));
    }

    @GetMapping("/updateFlag/{id}")
    @ApiOperation(value = "标记已读" , httpMethod = "GET")
    public JsonResult setFlag(@NotNull(message = "不能为空") @Min(message = "参数不能小于1", value = 1) @PathVariable(value = "id") Integer id,
                             HttpServletRequest httpServletRequest) {

        int userId = (int) httpServletRequest.getAttribute("id");

        return ResultUtils.success(rabbitMQService.updateFlag(id, userId), "修改成功");
    }


    //-----------管理员权限----------------------------


    @PostMapping("/sendMailbox")
    @ApiOperation(value = "管理员发送信息给用户（可群发）", httpMethod = "POST")
    public JsonResult send(@Validated @RequestBody MailboxVo vo) {

        rabbitMQService.adminMailbox(vo.getToId(), vo.getInfo());

        return ResultUtils.success(true, "请求已收到");

    }



}
